package com.dyrj.question.controller;

import com.dyrj.question.bean.PDayanswer;
import com.dyrj.question.dao.PDayawardMapper;
import com.dyrj.utils.DateUtil;
import com.dyrj.utils.ExcelExportHelper;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.activation.FileTypeMap;
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.RequestMapping;
import com.dyrj.vo.PageResultMsg;
import com.dyrj.vo.ResultMsg;
import com.dyrj.question.service.IPDayawardService;
import com.dyrj.question.bean.PDayaward;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;

/**
 * <p>
 * 日抽奖管理 前端控制器
 * </p>
 *
 * @author admin
 * @since 2021-04-30
 */
@Controller
@RequestMapping("/question/p-dayaward")
public class PDayawardController {

    @Resource
    private IPDayawardService service;
    @Resource
    private PDayawardMapper mapper;
    @GetMapping("/index")
//    @RequiresPermissions("question:p-dayaward:index")
    public String index(ModelMap map) {
    return "question/index_p-dayaward";
    }
    /**
    * @Title: table
    * @Description: (表格数据) 列表
    * @return PageInfo<PDayaward>    返回类型
    */
    @GetMapping("/table")
    @ResponseBody
//    @RequiresPermissions("question:p-dayaward:index")
    public PageResultMsg table(PDayaward record) {
        return service.table(record);
    }

    /**
    * @Title: add
    * @Description: (新增)
    * @param record
    * @return ResultMsg    返回类型
    */
    @PostMapping("/")
    @ResponseBody
    @RequiresPermissions("question:p-dayaward:add")
    public ResultMsg add(PDayaward record) {
        return service.add(record);
    }

    /**
    * @Title: info
    * @Description: (详情)
    * @param id
    * @return PDayaward    返回类型
    */
    @GetMapping("/{id}")
    @ResponseBody
    @RequiresPermissions("question:p-dayaward:info")
    public PDayaward info(@PathVariable String id) {
        return service.info(id);
    }

    /**
    * @Title: update
    * @Description: (更新)
    * @param record
    * @return ResultMsg    返回类型
    */
    @PutMapping("/")
    @ResponseBody
    @RequiresPermissions("question:p-dayaward:edit")
    public ResultMsg update(PDayaward record) {
        return service.update(record);
    }

    /**
    * @Title: delete
    * @Description: (删除)
    * @param id
    * @return ResultMsg    返回类型
    */
    @DeleteMapping("/{id}")
    @ResponseBody
    @RequiresPermissions("question:p-dayaward:delete")
    public ResultMsg delete(@PathVariable String id) {
        return service.delete(id);
    }

//    {type: 'numbers', title: '序号',fixed: 'left'},
//    {field:'awardtime', title: '抽奖时间', width:200},
//    {field:'usrname', title: '抽奖人姓名', width:100},
//    {field:'deptname', title: '部门名称', width:200},
//    {field:'award', title: '奖项', width:80},
//    {field:'bz', title: '备注', width:200},
    @GetMapping("/export")
    public  void export(@RequestParam(required = false) Map map, HttpServletResponse resp) throws Exception {
        ExcelExportHelper excelExportHelper = new ExcelExportHelper();
        String heads[] = {"抽奖时间","抽奖人姓名","部门名称","奖项","备注"};
        String [] properties={"awardtime1","usrname","deptname","award","bz"};
        String ksny = (String)map.get("ksny");
        String jsny = (String)map.get("jsny");
        PDayaward pDayaward = new PDayaward();
        pDayaward.setKsny(ksny);
        pDayaward.setJsny(jsny);
        List list = mapper.selectList(pDayaward);
        HSSFWorkbook hssfWorkbook = excelExportHelper.exportExcel(heads,properties,list,"每日答题情况统计");
        String filename = DateUtil.getAllTime()+"每日答题情况统计.xls";
        try {
            resp.setHeader("Content-Disposition", "attachment;filename="+filename);
            resp.setContentType(FileTypeMap.getDefaultFileTypeMap()
                    .getContentType(filename)+ "; charset=UTF-8");
            resp.setHeader("Content-disposition", "attachment; filename="
                    + new String(filename.getBytes("UTF-8"),
                    "ISO8859_1"));
            resp.setHeader("Cache-Control", "no-cache");
            resp.setDateHeader("Expires", 0);
            ServletOutputStream out =resp.getOutputStream();
            BufferedOutputStream bufferedOutPut = new BufferedOutputStream(out);
            bufferedOutPut.flush();
            hssfWorkbook.write(bufferedOutPut);
            bufferedOutPut.close();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (hssfWorkbook != null) {
                try {
                    hssfWorkbook.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
